//	ioctls.m
//
//  This file contains methods related to blacking out screens; this used
//  to be done via ioctls (hence the name) but for 3.0 there's a better
//  interface.
//
//  You may freely copy, distribute, and reuse the code in this example.
//  NeXT disclaims any warranty of any kind, expressed or  implied, as to its
//  fitness for any particular use.


#import "Thinker.h"
#import "BackWindow.h"
#import "psfuncts.h"
#import <appkit/Window.h>
#import <objc/List.h>
#import <libc.h>


@implementation Thinker(ioctls)

- normalMode
{
	[self _setDimBrightness : &dimBrightness];
	return self;
}

- screenSaverMode
{
	// this method prevents the screen from dimming so you can see the
	// screen saver.  Just return self right here if you want the screen
	// to go dim while the screensaver is doing its thing.

	[self getNormalBrightness :&normalBrightness];
	[self _setDimBrightness :&normalBrightness];

	return self;
}


// In the multi-headed case, I gotta throw a black window over all
// the screens so they don't burn in while I do animation on one.
// You'd want to black out all screen in every case if you switched
// animations on the fly to prevent the screen from possibly being
// unlocked for a moment.

// Hmm, I don't know why I didn't just put a single big non retained
// window over all screens instead...

- blackOutAllScreens
{
	int i;
	NXRect r;
		
	if (screenCount <= 1) return self;

	if (!screenList) screenList = [[List alloc] init];

	for (i=0; i < screenCount; i++)
	{
		id theWindow;
		r = screens[i].screenBounds;
			
		theWindow = [[BackWindow alloc]
				initContent:&r style:NX_TOKENSTYLE
				backing:NX_NONRETAINED buttonMask:0 defer:NO];

		[screenList addObject:theWindow];

		[theWindow removeFromEventMask:(NX_LMOUSEDOWNMASK | NX_LMOUSEUPMASK
  			   | NX_MOUSEMOVEDMASK | NX_LMOUSEDRAGGEDMASK
			   | NX_MOUSEENTEREDMASK | NX_MOUSEEXITEDMASK
			   | NX_KEYDOWNMASK | NX_KEYUPMASK
			   | NX_CURSORUPDATEMASK)];
		[theWindow setBackgroundGray:NX_BLACK];

		tweakWindow([theWindow windowNum], SAVERTIER-1);
		[theWindow placeWindowAndDisplay:&r];
		[theWindow orderFront:self];

	}

	return self;
}

- unBlackOutAllScreens
{
	if (screenCount <= 1) return self;
	[screenList makeObjectsPerform:@selector(orderOut:) with:self];
	[screenList freeObjects];
	return self;
}

- getDimBrightness:(double *)b
{
	*b = NXAutoDimBrightness(evs);
	return self;
}

- _setDimBrightness :(double *)b
{
	NXSetAutoDimBrightness(evs, *b);
	if (NXAutoDimState(evs))
	{
		NXSetAutoDimState(evs, NO);
		NXSetAutoDimState(evs, YES);
	}
	return self;
}


- getNormalBrightness :(double *)b
{
	*b = NXScreenBrightness(evs);
	return self;
}


- getDimTime :(double *)t
{
	*t = NXAutoDimTime(evs);
	return self;
}

@end

